//引入pinia
import {defineStore} from 'pinia'
//引入分类接口
import {getCategory1ListApi,getCategory2ListApi,getCategory3ListApi} from '@/api/product/category'
//引入分类相关的数据类型
import {CategoryIdModel,NumberModel,CategoryListModel,CategoryModel} from '@/api/product/model/categoryModel'

//pinia管理的分类状态
export interface CategoryStateModel extends CategoryIdModel {
    category1List:CategoryListModel
    category2List:CategoryListModel
    category3List:CategoryListModel
}


//创建仓库
export const useCategoryState = defineStore({
    id:'category',
    state:():CategoryStateModel =>({
        category1Id:undefined,  //一级分类id
        category2Id:undefined,  //二级分类id
        category3Id:undefined,  //三级分类id
        category1List:[], //一级分类的列表数据
        category2List:[], //一级分类的列表数据
        category3List:[], //一级分类的列表数据
    }),
    getters:{
        //获取一级分类id
        getCategory1Id():NumberModel{return this.category1Id as number},
        //获取二级分类id
        getCategory2Id():NumberModel{return this.category2Id as number},
        //获取三级分类id
        getCategory3Id():NumberModel{return this.category3Id as number},
        //获取一级分类列表
        getCategoryList1():CategoryListModel{return this.category1List},
        //获取二级分类列表
        getCategoryList2():CategoryListModel{return this.category2List},
        //获取三级分类列表
        getCategoryList3():CategoryListModel{return this.category3List},

    },
    actions:{
        //调用接口发送请求获取一级分类列表
        async getCategory1List(){
            this.category1List = await getCategory1ListApi()
        },
        //调用接口发送请求,根基一级分类id获取二级分类列表
        async getCategory2List(category1Id:number){
            //先存储一级分类id
            this.category1Id = category1Id
            this.category2List = await getCategory2ListApi(category1Id)
            //先干掉二级分类的id和三级分类的id以及三级分类列表数据
            this.category2Id = undefined
            this.category3Id = undefined
            // this.category1List = []
        },
        //调用接口发送请求,根据二级分类id获取三级分类列表
        async getCategory3List(category2Id:number){
            //先存储二级分类id
            this.category2Id = category2Id
            this.category3List = await getCategory3ListApi(category2Id)
            //干掉三级分类的id
            this.category3Id = undefined
        }
    }
})
export default useCategoryState
